package com.xb.cloud.merchantapi.adapter;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.merchantapi.product.service.ShopNavigationGroupApiService;
import com.xb.cloud.product.merchant.service.client.bo.ShopNavSkuRequestBo;
import com.xb.cloud.product.merchant.service.client.input.bo.ShopNavigationGroupInput;

/**
 * 
 * @author: 鲜备科技
 * @date: 2017年12月19日 下午6:30:04
 * 
 * @Copyright: 2017 www.xianbeitech.com Inc. All rights reserved.
 *
 */
@Component
public class ShopNavigationGroupAdapter extends Adapter {

	private static final Logger logger = LoggerFactory.getLogger(ShopNavigationGroupAdapter.class);

	@Resource
	private ShopNavigationGroupApiService shopNavigationGroupApiService;

	/**
	 * 
	 */
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		RtnInfo rtnInfo = null;

		/**
		 * 1. 创建店铺内分组
		 */
		if ("/h5/merchant/shopNavigationGroup/createShopNavicationGroup".equalsIgnoreCase(urlPath)) {
			ShopNavigationGroupInput groupInput = null;
			/**
			 * 参数验证
			 */
			try {
				String groupInputString = bizInfo.get("groupInput");
				groupInput = JSON.parseObject(groupInputString, ShopNavigationGroupInput.class);
				if (StringUtils.isEmpty(groupInputString)) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			Long endMerchantUserId = context.getCurrentMerchantUserId();
			// 调取Api service
			rtnInfo = shopNavigationGroupApiService.createShopNavicationGroup(groupInput, endMerchantUserId);

		}

		if ("/h5/merchant/shopNavigationGroup/getShopNavicationGroup".equalsIgnoreCase(urlPath)) {

			Long endMerchantUserId = context.getCurrentMerchantUserId();
			// 调取Api service
			rtnInfo = shopNavigationGroupApiService.getShopNavicationGroup(endMerchantUserId);
		}

		if ("/h5/merchant/shopNavigationGroup/deleteShopNavicationGroup".equalsIgnoreCase(urlPath)) {
			String groupId = bizInfo.get("groupId");
			rtnInfo = shopNavigationGroupApiService.deleteShopNavicationGroup(groupId);
		}

		if ("/h5/merchant/shopNavigationGroup/updateShopNavicationGroup".equalsIgnoreCase(urlPath)) {
			String groupId = bizInfo.get("groupId");
			ShopNavigationGroupInput groupInput = null;
			/**
			 * 参数验证
			 */
			try {
				String groupInputString = bizInfo.get("groupInput");
				groupInput = JSON.parseObject(groupInputString, ShopNavigationGroupInput.class);
				if (StringUtils.isEmpty(groupInputString)) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			Long endMerchantUserId = context.getCurrentMerchantUserId();
			rtnInfo = shopNavigationGroupApiService.updateShopNavicationGroup(groupId, groupInput, endMerchantUserId);

		}
		if ("/h5/merchant/shopNavigationGroup/skuList".equalsIgnoreCase(urlPath)) {
			ShopNavSkuRequestBo requestBo = new ShopNavSkuRequestBo();
			String companyId = bizInfo.get("companyId");
			if (!StringUtils.isEmpty(companyId)) {
				requestBo.setCompanyId(Long.valueOf(companyId));
			}

			String groupId = bizInfo.get("groupId");
			if (!StringUtils.isEmpty(groupId)) {
				requestBo.setGroupId(Long.valueOf(groupId));
			} else {
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			String shopId = bizInfo.get("shopId");
			if (!StringUtils.isEmpty(shopId)) {
				requestBo.setShopId(Long.valueOf(shopId));
			} else {
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			String pageNum = bizInfo.get("pageNum");
			if (!StringUtils.isEmpty(pageNum)) {
				requestBo.setPageNum(Integer.valueOf(pageNum));
			}
			String pageSize = bizInfo.get("pageSize");

			if (!StringUtils.isEmpty(pageSize)) {
				requestBo.setPageSize(Integer.valueOf(pageSize));
			}
			String type = bizInfo.get("type");
			if (!StringUtils.isEmpty(type)) {
				requestBo.setType(Integer.valueOf(type));
			}
			rtnInfo = shopNavigationGroupApiService.getSkuList(requestBo);
		}
		if ("/h5/merchant/shopNavigationGroup/add".equalsIgnoreCase(urlPath)) {

			String groupId = bizInfo.get("groupId");
			if (StringUtils.isEmpty(groupId)) {
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			String shopId = bizInfo.get("shopId");
			if (StringUtils.isEmpty(shopId)) {
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			String skuId = bizInfo.get("skuId");
			if (StringUtils.isEmpty(skuId)) {

				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			rtnInfo = shopNavigationGroupApiService.addSku(Long.valueOf(groupId), Long.valueOf(shopId),
					Long.valueOf(skuId));
		}
		if ("/h5/merchant/shopNavigationGroup/remove".equalsIgnoreCase(urlPath)) {
			String companyId = bizInfo.get("companyId");
			String groupId = bizInfo.get("groupId");
			if (StringUtils.isEmpty(groupId)) {
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			String shopId = bizInfo.get("shopId");
			if (StringUtils.isEmpty(shopId)) {
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			String skuId = bizInfo.get("skuId");
			if (StringUtils.isEmpty(skuId)) {

				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			rtnInfo = shopNavigationGroupApiService.removeSku(Long.valueOf(groupId), Long.valueOf(shopId),
					Long.valueOf(skuId));
		}
		
		if ("/h5/merchant/shopNavigationGroup/getGroupSkuCount".equalsIgnoreCase(urlPath)) {
			ShopNavSkuRequestBo requestBo = new ShopNavSkuRequestBo();
			String companyId = bizInfo.get("companyId");
			if (!StringUtils.isEmpty(companyId)) {
				requestBo.setCompanyId(Long.valueOf(companyId));
			}

			String groupId = bizInfo.get("groupId");
			if (!StringUtils.isEmpty(groupId)) {
				requestBo.setGroupId(Long.valueOf(groupId));
			} else {
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			String shopId = bizInfo.get("shopId");
			if (!StringUtils.isEmpty(shopId)) {
				requestBo.setShopId(Long.valueOf(shopId));
			} else {
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			String type = bizInfo.get("type");
			if (!StringUtils.isEmpty(type)) {
				requestBo.setType(Integer.valueOf(type));
			}
			rtnInfo = shopNavigationGroupApiService.getGroupSkuCount(requestBo);
		}
		
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/merchant/shopNavigationGroup/**";
	}

	@Override
	public boolean isCheckUserToken() {
		return true;
	}

}
